package educationTool.control;

import educationTool.model.Admin;
import educationTool.model.User;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Properties;

/**
 * Class for main application logic
 * 
 * @author Pavel
 */
public class Core {

	private static Core INSTANCE;

	private ArrayList<User> usersList = new ArrayList<User>();
	private User currentUser;
	private Admin admin = Admin.getInstance();
	Properties user_properties = new Properties();
	
	private Core(){
		admin = Admin.getInstance();
	}

	public static synchronized Core getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new Core();
		}
		return INSTANCE;
	}

	public boolean checkUserExistance(final User user) {		
		return usersList.contains(user) ? true : false;
	}
	
	public boolean checkUserExistance(final String login) {		
		for(User user : usersList){
			if(login.equals(user.getLogin()))
				return true;
		}
		return false;
	}

	public boolean logInAsAdmin(final String password) {
		return (admin.getPassword().equals(MD5(password))) ? true : false;
	}

	public boolean logInAsUser(String login, String password) {
		if (checkUserExistance(login)) {
			for (User user : usersList) {
				if (login.equals(user.getLogin())
						&& MD5(password).equals(user.getPassword())) {
					currentUser = user;
					return true;
				}
			}
		}

		return false;

	}

	public void logOut() {
		admin = null;
		currentUser = null;
	}

	private String MD5(String password) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(password.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}

	}

	public User getCurrentUser() {
		if (currentUser == null)
			throw new RuntimeException(
					"There is no current user! You didn\'t log "
							+ "in or you logged in as Admin");
		return currentUser;
	}

	public Admin getAdmin() {
		if (admin == null)
			throw new RuntimeException(
					"You didn\'t log in or you logged in as User");
		return admin;
	}

	public ArrayList<User> getUsersList() {
		return usersList;
	}

	/**
	 * @author Pavel This method is for storing users data in some file
	 * @exception IoException
	 *                is thrown if can't save into a file
	 * @return void Test version
	 */
	public void storeUsers() throws FileNotFoundException, IOException {
		/*
             * 
             */
		ArrayList<User> users = getUsersList();
		File file = new File("users.dat");
		FileWriter fw = new FileWriter(file);
		for (User i : users) {
			fw.write(i.toString());
		}
		fw.close();

	}

	/**
	 * Restores the users and their settings Test version
	 */
	public void restoreUsers() throws FileNotFoundException, IOException {

		File file = new File("users.dat");
		BufferedReader in = new BufferedReader(new FileReader(file));
		ArrayList<User> users = getUsersList();
		while (in.ready()) {

			String login = in.readLine();
			String password = in.readLine();
			users.add(new User(login, password));
		}
		in.close();

	}
}
